<?php $this->template->add_admin_js_lib('vendor/wizard/js/bwizard.min.js'); ?>

<!-- START panel-->
<div class="panel panel-default" id="box-seo">
	<div class="panel-heading">Tối ưu SEO</div>
	<div class="panel-body">
		<div data-toggle="wizard" class="form-wizard wizard-horizontal">
			<!-- START wizard steps indicator-->
			<ol class="row">
				<li class="col-lg-6">
					<h4>Google</h4>
					<small class="text-muted">Dữ liệu hiển thị trên Google</small>
				</li>
				<li class="col-lg-6">
					<h4>Facebook</h4>
					<small class="text-muted">Dữ liệu hiện thị khi share bài viết</small>
				</li>
			</ol>
			<!-- END wizard steps indicator-->
			<!-- START Wizard Step inputs -->
			<div>
				<fieldset><div class="form-group"><label for="boot_edit_seo_title" class="col-sm-2 control-label">Xem trước</label><div class="col-sm-10">
					<div id="wpseosnippet">
						<a class="title" id="wpseosnippet_title" href="#"></a>
						<span class="url"></span>
						<p class="desc"><span class="autogen"></span><span class="content"></span></p>
					</div>
				</div></div></fieldset>

				<?php echo $this->atheme->input('Keyword trọng tâm', 'metas[seo_keyword_focus]', set_value('metas[seo_keyword_focus]', @$post_metas['seo_keyword_focus'], array('help'=>'Keyword cần đánh mạnh trong bài viết, <br>Đánh giá keyword trọng tâm trong:<br>
					
					<b>Page title:</b> <span id="count-title" class="wrong">No</span><br>
					<b>Page URL:</b> <span id="count-url" class="wrong">No</span><br>
					<b>Content:</b> <span id="count-content" class="wrong">No</span><br>
					<b>Meta description:</b> <span id="count-desc" class="wrong">No</span><br>'))); ?>

					<?php echo $this->atheme->input('SEO Title', 'edit[seo_title]', set_value('edit[seo_title]', @$edit->seo_title), array('help'=>'')); ?>
					<?php echo $this->atheme->input('Meta description:', 'edit[seo_description]', set_value('edit[seo_description]', @$edit->seo_description), array('help'=>'Thẻ <em>meta</em> description giới hạn '.$this->seo_m->description_word_limit.' ký tự, bạn đã gõ <span class="good desc-length">145</span> ký tự.')); ?>
					<?php echo $this->atheme->input('Meta keywords:', 'edit[seo_keyword]', set_value('edit[seo_keyword]', @$edit->seo_keyword), array('help'=>'Từ khóa của bài viết, mỗi từ khóa cách nhau bởi dấu phẩy (,)')); ?>
				</div>
				<!-- END Wizard Step inputs -->
				
				<!-- START Wizard Step inputs -->
				<div>
				<div class="alert alert-danger alert-dismissable"> <button type="button" data-dismiss="alert" aria-hidden="true" class="close">×</button>Lưu ý: Phải lưu bài xong thì chức năng này mới có hiệu quả.</div>
				
				<?php echo $this->atheme->input('Facebook Title:', 'metas[_cms_post_fb_title]', set_value('metas[_cms_post_fb_title]', @$post_metas['_cms_post_fb_title']), array('help'=>'Tiêu đề bài viết khi share, nếu để trống sẽ lấy mặc định tiêu đề')); ?>
				<?php echo $this->atheme->textarea('Facebook Description', 'metas[_cms_post_fb_description]', set_value('metas[_cms_post_fb_description]', @$post_metas['_cms_post_fb_description']), array('rows'=>5, 'help'=>'')); ?>
				<?php echo $this->atheme->ajax_upload('_cms_post_fb_thumb', 'Ảnh dại diện', '_cms_post_fb_thumb', 'span6 ', image(@$post_metas['_cms_post_fb_thumb']), 'text',80,80, 'allowfile="jpg,png,gif" ','Hình đại diện'); ?>
				<div style="text-align: center;">
				<a class="btn btn-default" href="https://www.facebook.com/sharer/sharer.php?u=http%3A%2F%2Fsao.vui.us%2F" target="_blank">Xem thử</a>
				<input type="button" value="Xóa cache Facebook" class="btn btn-default" id="fb-clear-cache">
				</div>
				</div>
				<!-- END Wizard Step inputs -->

			</div>

		</div>
	</div>
	<!-- END panel -->

	<style>
		#wpseosnippet {
			width: auto;
			max-width: 520px;
			margin: 0 0 10px;
			padding: 0 5px;
			font-family: Arial,Helvetica,sans-serif;
			font-style: normal;
		}
		#wpseosnippet .title {
			display: block;
			overflow: hidden;
			width: 512px;
			color: #1e0fbe;
			font-size: 18px!important;
			line-height: 1.2;
			white-space: nowrap;
			text-overflow: ellipsis;
		}
		#wpseosnippet a:hover {
			text-decoration: underline;
		}
		#wpseosnippet a {
			text-decoration: none;
		}
		#wpseosnippet .url {
			color: #006621;
			font-size: 13px;
			line-height: 16px;
		}

		#wpseosnippet .desc {
			font-size: small;
			line-height: 1.4;
			word-wrap: break-word;
		}
		#wpseosnippet p {
			margin: 0!important;
		}
		#box-seo .good {
			color: green;
			font-weight: 700;
		}
		#box-seo .wrong {
			color: red;
		}
		#box-seo .well {
			  padding-top: 15px;
		}
	</style>
	<script type="text/javascript" charset="utf-8">
		$(document).ready(function(){

			function containsWord(haystack, needle) {
				return (" " + haystack + " ").indexOf("" + needle + " ") > -1;
			}

			function countInstances(string, word) {
				var substrings = string.split(word);
				return substrings.length - 1;
			}


			//check title
			$('#boot_edit_post_title, #boot_edit_seo_title').keyup(function () {
				if($("#boot_edit_seo_title").val() =="")
				{
					var str = $("#boot_edit_post_title").val();
				}
				else
				{
					var str = $("#boot_edit_seo_title").val();
				}
				$("#boot_edit_seo_title").attr("placeholder", str);
				$("#wpseosnippet_title").text(str);
				$('#boot_edit_seo_keyword_focus').trigger('keyup');
			});

			$('#boot_edit_seo_keyword_focus').keyup(function () {
				var val = $(this).val().toLowerCase();
				var page_title = $("#wpseosnippet #wpseosnippet_title").text().toLowerCase();
				var page_desc = $("#wpseosnippet .content").text().toLowerCase();
				var page_content = $("#post_content").text().toLowerCase();
				if(val =='')
				{
					$("#boot_edit_seo_keyword_focus").siblings(".help-block").hide();
				}
				else
				{
					$("#boot_edit_seo_keyword_focus").siblings(".help-block").show();
				}

				 //title check
				 if(containsWord(page_title, val) == true)
				 {
				 	$("#count-title").text("Yes ("+countInstances(page_title, val)+")");
				 	$("#count-title").attr("class","good");
				 }
				 else
				 {
				 	$("#count-title").attr("class","wrong");
				 	$("#count-title").text("No");
				 }

				 //description check
				 if(containsWord(page_desc, val) == true)
				 {
				 	$("#count-desc").text("Yes ("+countInstances(page_desc, val)+")");
				 	$("#count-desc").attr("class","good");
				 }
				 else
				 {
				 	$("#count-desc").attr("class","wrong");
				 	$("#count-desc").text("No");
				 }
				 
				 //description check
				 if(containsWord(page_content, val) == true)
				 {
				 	$("#count-content").text("Yes ("+countInstances(page_content, val)+")");
				 	$("#count-content").attr("class","good");
				 }
				 else
				 {
				 	$("#count-content").attr("class","wrong");
				 	$("#count-content").text("No");
				 }


				});

$('#boot_edit_post_excerpt, #boot_edit_seo_description').keyup(function () {
	var strlength_limit = <?php echo $this->seo_m->description_word_limit;?>;
	if($("#boot_edit_seo_description").val() =="")
	{
		var str = $("#boot_edit_post_excerpt").val();

	}
	else
	{
		var str = $("#boot_edit_seo_description").val();
	}
	var strlength = str.length;


	if(str.length > strlength_limit)
	{
		$(".desc-length").removeClass("good");
		$(".desc-length").addClass("wrong");
		$("#wpseosnippet span.content").text(str);
		strlength = "-"+strlength;
		str = str.substring(0, strlength_limit);
	}
	else
	{
		$(".desc-length").addClass("good");
		$(".desc-length").removeClass("wrong");
	}
	$("#wpseosnippet span.content").text(str);
	$("#box-seo span.desc-length").text(strlength);
	$('#boot_edit_seo_keyword_focus').trigger('keyup');
});

			//check url
			$('#boot_edit_post_slug').keyup(function () {
				var str = '/'+$("#boot_edit_post_slug").val()+'-'+edit_id+'.html';
				$("#wpseosnippet span.url").text(str);
				$('#boot_edit_seo_keyword_focus').trigger('keyup');
			});


			$('#boot_edit_post_title').trigger('keyup');
			$('#boot_edit_post_excerpt').trigger('keyup');
			$('#boot_edit_post_slug').trigger('keyup');
			$('#boot_edit_seo_keyword_focus').trigger('keyup');
			//check url
			$('#fb-clear-cache').click(function () {
				$.post(
				'https://graph.facebook.com',
				{
					id: '<?php echo post_url($edit);?>',
					scrape: true
				},
				function(response){
					if(response.id)
					{
						$.notify("Xóa cache thành công.",'success');
					}
					console.log(response);
				}
				);
			});
			
		});

</script>